package com.pingan.cashier.web;

import com.pingan.cashier.common.config.Constants;
import com.pingan.cashier.service.CallbackService;
import com.pingan.cashier.service.dto.BaseCallBackDTO;
import com.pingan.cashier.web.req.AlipayResultReq;
import com.pingan.cashier.web.resp.CommonResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Constraint;
import javax.validation.Valid;
import java.util.List;

/**
 * 支付宝回调
 */
@RestController
@Slf4j
@RequestMapping("/alipay")
public class AlipayCallbackController {

    @Autowired
    private List<CallbackService> callbackServiceList;

    /**
     * 阿里支付回调
     * @return
     */
    @PostMapping("/result")
    @ResponseBody
    public CommonResp callback(@Valid @RequestBody AlipayResultReq req){

        CallbackService callbackService = callbackServiceList.stream().filter(d->d.supportPayType(Constants.PAY_TYPE_ZFB))
                .findFirst().orElse(null);
        if(callbackService == null){
            throw new RuntimeException("暂不支持支付宝回调");
        }
        callbackService.callback(BaseCallBackDTO.builder()
                .payType(Constants.PAY_TYPE_ZFB)
                .tradeNo(req.getTradeNo())
                .tradeStatus(req.getTradeStatus())
                .build());
        return CommonResp.success();
    }
}
